package com.vaadin.demo.workoutlog;

import com.vaadin.data.CayenneContainer;
import com.vaadin.demo.workoutlog.pojo.Type;
import com.vaadin.demo.workoutlog.pojo.Workout;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import org.apache.cayenne.DataObjectUtils;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;

import java.util.Date;

public class WorkoutEditor extends Window implements ClickListener {

  private DateField date = new DateField("Date");
  private TextField kilomiters = new TextField("Kilometers");
  private TextField title = new TextField("Title/note");
  private ComboBox types = new ComboBox("Workout type");

  private Button save = new Button("Save");
  private Button delete = new Button("Delete");

  private Workout run;
  private WorkoutLog workoutLog;

  public WorkoutEditor(WorkoutLog app) {
    super("Edit workout");
    workoutLog = app;
    Layout main = new VerticalLayout();
    setContent(main);
    main.setSizeUndefined();
    main.setStyleName(Panel.STYLE_LIGHT);
    types.setContainerDataSource(new CayenneContainer<Type>(Type.class, "title", app));
    types.setItemCaptionPropertyId("title");
    types.setNewItemsAllowed(false);

    FormLayout form = new FormLayout();
    form.setSizeUndefined();
    date.setResolution(DateField.RESOLUTION_MIN);
    form.addComponent(date);
    form.addComponent(kilomiters);
    form.addComponent(title);
    form.addComponent(types);
    main.addComponent(form);

    HorizontalLayout actions = new HorizontalLayout();
    actions.addComponent(save);

    save.addListener(this);

    actions.addComponent(delete);
    delete.addListener(this);

    main.addComponent(actions);
  }

  public void loadWorkout(Workout run) {
    if (run == null) {
      close();
    }
    else {
      date.setValue(run.getDate());
      kilomiters.setValue(run.getKilometers());
      title.setValue(run.getTitle());
      ((CayenneContainer<Type>)types.getContainerDataSource()).refresh();
      if (run.getTrainingType() != null) {
        types.setValue(run.getTrainingType().getObjectId());
      }
      if (getParent() == null) {
        workoutLog.getMainWindow().addWindow(this);
      }
      kilomiters.focus();
      this.run = run;
    }
  }

  public void buttonClick(ClickEvent event) {
    if (event.getButton() == delete) {
      workoutLog.deleteRun(run);
    }
    else if (event.getButton() == save) {
      run.setDate((Date)date.getValue());
      run.setKilometers(Float
                          .parseFloat(kilomiters.getValue().toString()));
      run.setTitle((String)title.getValue());

      Type selectedType = (Type)DataObjectUtils.objectForPK(workoutLog.getCayenneContext(), (ObjectId)types.getValue());
      run.setTrainingType(selectedType);

      int state = run.getPersistenceState();

      if (state == PersistenceState.TRANSIENT || state == PersistenceState.NEW) {
        workoutLog.addNewWorkout(run);
      }
      else {
        workoutLog.updateExistingWorkout(run);
      }
    }
    if (getParent() != null) {
      getParent().removeWindow(this);
    }
  }
}
